Lesson 4 - intermediate exercise

For each scenario suggested below create the corresponding set of if statements. If you are finding it difficult you should create a truth tree diagram.


def sum(listToAdd, ignoreNegative)
	total = 0
	for i in listToAdd:
		# will add i to total but if ignoreNegative is true
		# then it will skip over any value which is <0
	return total

sum([3,-3,3,-3], False) # prints 0
sum([3,-3,3,-3], True) # prints 6

Suggested change- You need to do 2 tests but that does not mean it needs to be a nested if!

Toggle answer

skinny = input("Do you want your coffee skinny?")
type = input("Do you want a latte, espresso or a cappuccino?")
chocolate = input("Do you want a shot of hot chocolate?")

# the table below shows what should be outputted
# -----------------------------------------------------------------------
# * skinny * type             * chocolate * output                       *
# -----------------------------------------------------------------------
# * no     * latte            * no       * latte                        *
# ***********************************************************************
# * no     * espresso         * no       * espresso                     *
# ***********************************************************************
# * no     * cappuccino       * no       * cappuccino                   *
# ***********************************************************************
# * yes    * latte            * no       * skinny latte                 *
# ***********************************************************************
# * yes    * espresso         * no       * skinny espresso              *
# ***********************************************************************
# * yes    * cappuccino       * no       * skinny cappuccino            *
# ***********************************************************************
# * no     * latte            * yes      * Mochachino                   *
# ***********************************************************************
# * no     * espresso         * yes      * not served                   *
# ***********************************************************************
# * no     * cappuccino       * yes      * not served                   *
# ***********************************************************************
# * yes    * latte            * yes      * skinny Mochachino            *
# ***********************************************************************
# * yes    * espresso         * yes      * not served                   *
# ***********************************************************************
# * yes    * cappuccino       * yes      * not served                   *
# ***********************************************************************
 

Suggested change - You need to test the input and produce the correct output for the input above. If you get a unknown value then it should print "not served". There are many ways to solve the above problem so do not worry if your solution does not look like the model answer.

Toggle answer